// PROGRAMA : SCRPP004 OPCAO: "CHE"
// SISTEMA : SISP - SISTEMA DE INFORMACOES DE SEGURANCA PUBLICA
// IDENTIFICACAO CRIMINAL
// BIBLIOTECA : SISP
// CLIENTE : GEJUSPC - GERENCIA DE ESTADO DA JUSTICA, SEGURANCA
// PUBLICA E CIDADANIA / INSTITUTO DE IDENT.
// OBJETIVO : HISTORICO DE EMISSOES DE ATESTADOS DE
// ANTECEDENTTES PERIODO E USUARIO. INFORME:'CHE'
// PARA O SHOPPING DO CIDADAO
// PROGRAMADOR: BRUNO LOPES
// DATA : 28 DE JANEIRO DE 1998
// ALTERACAO : 04/02/1998 POR SOLON MIRANDA
// : TELA E DATA(N6) PARA (N8)

package br.com.vti.sisp_sicrim.antigo.controller.programa;

import java.text.ParseException;

import net.sf.jasperreports.engine.JRException;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.interceptor.download.InputStreamDownload;
import br.com.vti.sisp_sicrim.antigo.controller.relatorios.Scrpp004Report;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Scrpp004A;
import br.com.vti.sisp_sicrim.antigo.model.DidEmissao;
import br.com.vti.sisp_sicrim.antigo.model.SicEstatistica;
import br.com.vti.sisp_sicrim.antigo.util.ExceptionProgram;
import br.com.vti.sisp_sicrim.antigo.util.Scrg001;
import br.com.vti.sisp_sicrim.antigo.util.Sessao;
import br.com.vti.sisp_sicrim.antigo.util.UtilBean;

/**
 * Classe : Scrpp004 OBJETIVO : HISTORICO DE EMISSOES DE ATESTADOS DE ANTECEDENTTES PERIODO E USUARIO. INFORME:'CHE' PARA O SHOPPING DO CIDADAO *
 * 
 * @author Incode - Data: mai/2014
 * 
 */

@Resource
@Path("/antigo/scrpp004")
public class Scrpp004 {
	Scrpp004 scrpp004Anterior;
	private String faseproc = "0";
	// Views utilizadas
	SicEstatistica vwCadest = new SicEstatistica();
	DidEmissao vwEmissao = new DidEmissao();
	// Telas/relatorios utilizados
	Scrpp004A scrpp004A;
	// Scrpp004B scrpp004B;
	// Scrpp004C scrpp004C;
	Scrpp004Report report;
	// Includes utilizados
	// Sislct01 sislct01 = new Sislct01();
	// Sisldat8 sisldat8 = new Sisldat8();
	// Areas globais / session
	Scrg001 scrg001; // Funcoes/subrotinas
	FuncoesGerais funcoes = new FuncoesGerais();

	Result result;
	Sessao sessao;
	Boolean primeiraVez = false;

	private int jCont = 0;
	private String[] tabm = { "", "   ", "VEI", "CON", "MUL", "   ", "IPV", "ROU", "   ", "   ", "   ", "   ", "   ", "IDC", "ICR", "FUN", "   ", "   " };

	private Scrpp004A preMapas() {
		sessao.getScrg001().setWVltw(5);
		sessao.getScrg001().setWProgramaw(this.getClass().getSimpleName().toUpperCase());
		scrpp004A = new Scrpp004A(true, sessao);
		scrpp004A.setDataCorrente(UtilBean.obtemDataDia());
		scrpp004A.setNomeClasse(this.getClass().getSimpleName().toUpperCase());
		// Par�grafo: rp0()
		return scrpp004A;
	}

	public Scrpp004(Result result, Sessao sessao) {
		this.result = result;
		this.sessao = sessao;
	}

	@Post
	public InputStreamDownload executar(Scrpp004A scrpp004A) {
		try {
			return main(scrpp004A);
		} catch (Exception e) {
			ExceptionProgram excp = new ExceptionProgram();
			excp.trataErroScrcerro(e, sessao, this.getClass().getSimpleName());
		}
		return null;
	}

	private InputStreamDownload main(Scrpp004A pscrpp004A) throws Exception {
		if (pscrpp004A == null) {
			primeiraVez = true;
			// scrpp004A = new Scrpp004A (primeiraVez);
			scrpp004A = preMapas();
			scrpp004A.setNavegar("../scrpp004/executar");
			result.include("scrpp004A", scrpp004A);
			sessao.setAtributo("Scrpp004", this);
			scrpp004A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return null;
		} else {
			restaurarEstados(pscrpp004A);
		}

		scrpp004A.receberTela(false, result);

		if (UtilBean.funcao.equals(UtilBean.ctt_retorna)) {
			Scrpp001 scrpp001 = new Scrpp001(null, null);
			scrpp001.executar(null);
		}

		if (!UtilBean.validarMascara(scrpp004A.getDatini(), "yyyyMMdd")) {
			scrpp004A.enviarMensagem("ddt", "");
			result.include("scrpp004A", scrpp004A);
			sessao.setAtributo("Scrpp004", this);
			scrpp004A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return null;
		}

		if (!UtilBean.validarMascara(scrpp004A.getDatfim(), "yyyyMMdd")) {
			scrpp004A.enviarMensagem("dfi", "");
			result.include("scrpp004A", scrpp004A);
			sessao.setAtributo("Scrpp004", this);
			scrpp004A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return null;
		}

		if (scrpp004A.getDatini() > scrpp004A.getDatfim()) {
			scrpp004A.enviarMensagem("dfi", "");
			result.include("scrpp004A", scrpp004A);
			sessao.setAtributo("Scrpp004", this);
			scrpp004A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return null;
		}

		// //DEFINE PRINTER(EMF = 1)
		// OUTPUT 'P69C'
		// RELATORIO 1 PS=60 LS=132 SG=OFF
		report = new Scrpp004Report();
		report.setPageSize(60);
		report.setLineSize(132);
		// RELATORIO

		report.setJusuario(scrpp004A.getJusuario());
		report.setDataCorrente(scrpp004A.getDataCorrente());
		report.setDtini(scrpp004A.getDatini());
		report.setDtini((int) funcoes.sissd002(report.getDtini()));
		report.setDtfim(scrpp004A.getDatfim());
		report.setDtfim((int) funcoes.sissd002(report.getDtfim()));
		report.setJcont(0);

		detalhe();
		rodape();
		return report.imprimir();
	}

	private void detalhe() throws Exception {
		if (scrpp004A.getJusuario() != 0) {
			while (vwEmissao.leituraDataEmissaoFaixa(scrpp004A.getDatini(), scrpp004A.getDatfim())) {
				// scrpp004A.enviarReceberTela(false, result);
				if (vwEmissao.getMatriculaFunc() == scrpp004A.getJusuario() && vwEmissao.getTipReg() == 3) {
					dados();
				}
			}
			vwEmissao.fecha();
		} else {
			while (vwEmissao.leituraDataEmissaoFaixa(scrpp004A.getDatini(), scrpp004A.getDatfim())) {
				// scrpp004A.enviarReceberTela(false, result);
				if (vwEmissao.getTipReg() == 3) {
					dados();
				}
			}
			vwEmissao.fecha();
		}

	}

	private void dados() throws Exception {
		report.setDtini(vwEmissao.getDataEmissao());
		report.setDtini((int) funcoes.sissd002(report.getDtini()));

		report.setJnomeide(vwEmissao.getNomeIdent());
		report.setDidemiHoremi(vwEmissao.getHoraEmissaoCarteira());
		report.setDidemiMatfun(vwEmissao.getMatriculaFunc());
		report.setDidemiNserie(vwEmissao.getNumeroSerieRg());
		report.setDidemiRegger(vwEmissao.getRegGeralEmitido());
		report.setJusuario(scrpp004A.getJusuario());
		report.setDataCorrente(scrpp004A.getDataCorrente());

		report.gravaRelatorio(2);

		jCont++;
		report.setJcont(jCont);
	}

	private void rodape() throws ParseException, JRException {
		report.gravaRelatorio(3);
	}

	private void restaurarEstados(Scrpp004A pscrpp004A) {
		this.scrpp004Anterior = (Scrpp004) sessao.getAtributo("Scrpp004");
		if (pscrpp004A != null) {
			scrpp004A = pscrpp004A;
			scrpp004A.setJempresaj(sessao.getScrg001().getWEmpresaw());
			scrpp004A.setjVersaoj(sessao.getScrg001().getWVersaow());
			scrpp004A.setjProgramaj(sessao.getScrg001().getWProgramaw());
		} else {
			scrpp004A = this.scrpp004Anterior.scrpp004A;
		}
		scrpp004A.setSessao(sessao);
		this.faseproc = this.scrpp004Anterior.faseproc;
	}
}
